package com.linkgie.galaxyframework.utils;

/**
 * 运行时状态码；
 * 
 * @author Hike
 *
 */
public enum RuntimeStatus {

	SUCCESS(0, "成功"),

	SERVICE_POLICY_REJECT(10200, "平台策略拒绝"),

	INVALID_PARAMETERS(10201, "请求参数不合格"),

	REDIRECT(10302, "重定向"),

	BAD_REQUEST(10400, "请求异常"),

	/**
	 * 系统层面的超时；
	 */
	TIMEOUT(10401, "系统超时"),

	/**
	 * 系统发生中断信号导致处理中断；
	 */
	INTERRUPTED(10402, "系统中断"),


	INVALID_BUSINESS_STATE(10416, "无效业务状态"),

	TARGET_NOT_FOUND(10417, "未找到操作目标"),

	VIOLATION_OF_BUSINESS_RULE(10418, "违反业务规则"),

	INVALID_ACCOUNT(10419, "无效账户"),

	RESOURCE_EXPIRED(10420, "资源过期"),

	DATA_VALIDATION_FAIL(10421, "数据校验不通过"),

	DATA_CONFLICT(10422, "数据冲突"),

	/**
	 * 业务规则层面的超时；
	 */
	BUSINESS_TIMEOUT(10423, "业务超时"),

	SERVER_INNER_ERROR(10500, "服务器内部错误"),

	ILLEGAL_RUNTIME_CONTEXT(10501, "非法的运行时上下文"),

	//--------------------------------------
	// 安全授权相关；
	//--------------------------------------

	UNAUTHENTICATED(10710, "未获得认证"),

	UNAUTHORIZED(10711, "未获得授权"),

	LOCKED_USER(10712, "账号已被锁定"),

	NO_CREDENTIALS(10713, "缺少凭证"),

	BAD_CREDENTIALS(10714, "无效凭证"),

	TOKEN_EXPIRED(10715, "令牌过期"),

	INVALID_VERIFICATION_CODE(10730, "无效验证码"),

	

	// --------------------------------------------------------
	// License 许可相关的异常编码的范围控制在 10700[LICENSE_UNAUTHORIZED] ~
	// 10799[LICENSE_ILLEGAL] 之间；
	
	LICENSE_UNAUTHORIZED(11700, "功能未被许可"),

	LICENSE_USAGE_OUT_OF_LIMITS(11701, "用量超出许可限制"),

	LICENSE_USAGE_EXPIRED(11702, "功能许可过期"),
	
	LICENSE_RULE_UNSUPPORTED(11798, "不支持的许可规则"),

	/**
	 * 非法的许可证；
	 */
	LICENSE_ILLEGAL(11799, "非法的许可证");
	// --------------------------------------------------------

	public final int CODE;

	public final String DESCRIPTION;

	private RuntimeStatus(int code, String description) {
		this.CODE = code;
		this.DESCRIPTION = description;
	}

	/**
	 * 返回指定代码的状态枚举；
	 * <p>
	 * 如果没有，则返回 null；
	 * 
	 * @param code
	 * @return
	 */
	public static RuntimeStatus valueOf(int code) {
		for (RuntimeStatus status : RuntimeStatus.values()) {
			if (code == status.CODE) {
				return status;
			}
		}
		return null;
	}

	public static boolean isLicense(int code) {
		return code >= RuntimeStatus.LICENSE_UNAUTHORIZED.CODE && code <= RuntimeStatus.LICENSE_ILLEGAL.CODE;
	}
}
